package com.iwords.android;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.content.Context;

public class ServerAccessor {
	
	//private
	static JSONObject access(Context context, HttpUriRequest request) throws Exception {
		// 发送请求
		HttpResponse httpResponse = new DefaultHttpClient().execute(request);
		// 得到应答的字符串，这也是 JSON 格式保存的数据
		String retSrc = EntityUtils.toString(httpResponse.getEntity());
		//System.out.println("&&&&&&&&&&&&SreverAccessor.access() request.getURI() = "+request.getURI());
		//System.out.println("&&&&&&&&&&&&SreverAccessor.access() retSrc = "+retSrc);
		//new Exception().printStackTrace();
		// 生成 JSON 对象
		JSONObject jSONObject = new JSONObject(retSrc);
		//if(jSONObject.has("needPay")) throw new NeedPayException();//抛出NeedPayException将会调用老的付费界面(AlipayActivity)
		if(jSONObject.has("needPay")) {
			IWordsApp iWordsApp = (IWordsApp) context.getApplicationContext();
			iWordsApp.setNeedPay(true);
		}
		//System.out.println("&&&&&&&&&&&&SreverAccessor.access() did not throw NeedPayException.");
		return jSONObject;
	}

}
